package com.shushan.lockscreen;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import com.android.permission.FloatPermissionUtil;
import com.shushan.base.Constants;
import com.shushan.ui.CountdownTextView;
import com.shushan.util.SharedPreferencesUtils;
import com.shushan.util.ToastUtils;

/**
 * @ClassName: EyeSleepWindowManager
 * @Desciption: 视力保护悬浮窗管理类
 * @author: yichaohua
 * @date: 2018-08-06
 */
public class EyeSleepWindowManager {

    private static final String TAG = "EyeSleepWindowManager";

    private static EyeSleepFloatWindow eyeSleepFloatWindow;
    private static WindowManager.LayoutParams eyeSleepWindowParams;
    private static WindowManager eyeSleepWindowManager;
    private static boolean isEyeSleepAvailable = false;

    public static void createEyeFloatWindow(Context context) {
        Log.i(TAG,"createEyeFloatWindow()");

        if(context == null){
            Log.e(TAG,"context == null");
            return;
        }

        if(eyeSleepWindowManager == null){
            eyeSleepWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }

        if(eyeSleepWindowParams == null){
            eyeSleepWindowParams = new WindowManager.LayoutParams();
        }

        if(eyeSleepFloatWindow == null){
            eyeSleepFloatWindow = new EyeSleepFloatWindow(context);
        }else {
            Log.i(TAG,"setStartGone()");
            eyeSleepFloatWindow.setStartGone();
        }

        eyeSleepWindowParams.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){//8.0
            eyeSleepWindowParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        }else {
            eyeSleepWindowParams.type =  WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }
        eyeSleepWindowParams.format = PixelFormat.TRANSPARENT;
        eyeSleepWindowParams.gravity = Gravity.TOP;
        eyeSleepWindowParams.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN |
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
        eyeSleepWindowParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        eyeSleepWindowParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        //强制竖屏
        eyeSleepWindowParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;

        if(FloatPermissionUtil.checkPermission(context)){

            if(isEyeSleepAvailable){
                try {
                    eyeSleepWindowManager.updateViewLayout(eyeSleepFloatWindow, eyeSleepWindowParams);
                }catch (IllegalArgumentException e){
                    e.printStackTrace();
                    eyeSleepWindowManager.addView(eyeSleepFloatWindow, eyeSleepWindowParams);
                }

            }else {
                try {
                    eyeSleepWindowManager.addView(eyeSleepFloatWindow, eyeSleepWindowParams);
                }catch (IllegalStateException e){
                    e.printStackTrace();
                    eyeSleepWindowManager.removeView(eyeSleepFloatWindow);
                    eyeSleepWindowManager.addView(eyeSleepFloatWindow, eyeSleepWindowParams);
                }

            }

            CountdownTextView countdownTextView = eyeSleepFloatWindow.getCountdownTextView();
            if(countdownTextView != null){
                countdownTextView.setTimes(5*60);
            }

            isEyeSleepAvailable = true;
        }else {
                ToastUtils.showToast(context,"书山宝您的手机没有授予悬浮窗权限，请开启后再试",5);
        }
    }

    public static void removeEyeFloatWindow(Context context) {
        Log.i(TAG,"removeEyeFloatWindow()");
        if(eyeSleepWindowManager == null){
            eyeSleepWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        if(isEyeSleepAvailable){
            try {
                isEyeSleepAvailable = false;
                eyeSleepWindowManager.removeView(eyeSleepFloatWindow);
            }catch (IllegalArgumentException e){
                e.printStackTrace();
            }

            CountdownTextView countdownTextView = eyeSleepFloatWindow.getCountdownTextView();
            if(countdownTextView != null){
                countdownTextView.stop();
            }
        }

    }

    public static EyeSleepFloatWindow getEyeSleepFloatWindow() {
        return eyeSleepFloatWindow;
    }

    public static void setEyeSleepFloatWindow(EyeSleepFloatWindow eyeSleepFloatWindow) {
        EyeSleepWindowManager.eyeSleepFloatWindow = eyeSleepFloatWindow;
    }

    public static boolean isIsEyeSleepAvailable() {
        return isEyeSleepAvailable;
    }

    public static void setIsEyeSleepAvailable(boolean isEyeSleepAvailable) {
        EyeSleepWindowManager.isEyeSleepAvailable = isEyeSleepAvailable;
    }
}
